package com.onepiece.gen.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.onepiece.gen.generator.engine.MybatisPlusTemplateEngine;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class MybatisPlusFastAutoGenerator {

    private static final String dbUrl = "jdbc:mysql://localhost:3306/gen?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false";
    private static final String userName = "root";
    private static final String password = "123456";
    private static String moduleName = "/system";
    private static String[] tableNames ;
    private static String level ;

    private static final String ABSTRACT_PATH = System.getProperty("user.dir");
    private static final String projectName = "/gen-base-svc";
    private static final String MAVEN_PROJECT_JAVA_PATH = "/src/main/java";
    private static final String MAVEN_PROJECT_RESOURCE_PATH = "/src/main/resources";
    private static final String subProjectName = "admin";
    private static final String basePackages = "com.onepiece.gen.base.svc.business";

    private static final String fullProjectName = ABSTRACT_PATH + projectName;

    private static final String mapperXmlPath = fullProjectName + MAVEN_PROJECT_RESOURCE_PATH ;

    private static Info genInfo ;
    public static void setInfo (Info info){
        genInfo = info;
    }

    public static void main(String[] args) {

        moduleName = scanner("模块名:");
        tableNames = scanner("表名，多个英文逗号分割").split(",");
        level = scanner("分层：svc");

        FastAutoGenerator.create(dbUrl , userName , password )
                .templateEngine(new MybatisPlusTemplateEngine())
                .globalConfig(builder -> {
                    builder.author("tangld")
                            .outputDir(fullProjectName+MAVEN_PROJECT_JAVA_PATH)
                            .fileOverride()
                            .disableOpenDir()
                            .build();
                })
                .packageConfig(builder -> {
                    String pathInfo = "entity";
                    Map<OutputFile, String> pathInfoMap = new HashMap<OutputFile , String>();

                    pathInfoMap.put(OutputFile.mapperXml , mapperXmlPath+"/mapper/"+changePackageToReasourcePath(basePackages)+"/"+moduleName+"/");
                    builder.parent(basePackages)
                            .moduleName(moduleName)
                            .pathInfo(pathInfoMap);

                    PackageConfig packageConfig = builder.build();
                    String otherPathInfo = fullProjectName + MAVEN_PROJECT_JAVA_PATH + File.separator
                            + packageConfig.getParent().replace(".","/")+ File.separator+pathInfo;
                    packageConfig.getPathInfo().put(OutputFile.other,otherPathInfo);
                    if("svc".equals(level)){
                        packageConfig.getPathInfo().put(OutputFile.controller,"");
                    }
                })
                .injectionConfig(builder -> {
                    /*Map<String, String> customFile = new HashMap<String, String>();
                    customFile.put("DTO" , "/templates/EntityDTO.java.vm");
                    builder.customFile(customFile)
                            .beforeOutputFile((tableInfo , ObjectMap) -> {
                                ObjectMap.put("swagger2", true);
                            });*/
                })
                .templateConfig(builder -> {

                })
                .strategyConfig(builder -> {
                    builder.enableCapitalMode()
                            .addTablePrefix("gen")
                            .addInclude(tableNames)
                            .mapperBuilder()
//                                .superClass(CoreBaseMapper.class)
                            .controllerBuilder()
                                .enableRestStyle()
                            .serviceBuilder()
//                                .superServiceClass(ICoreService.class)
//                                .superServiceImplClass(CoreServiceImpl.class)
                            .entityBuilder()
                                .enableTableFieldAnnotation()
                                .enableLombok()
                                .enableChainModel()
//                                .superClass(BaseEntity.class)
//                                .addSuperEntityColumns(new String[]{"state" ,"create_time" ,"creator","update_time" ,"updator"})
                            .build();
                })

                .execute();
    }

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }
    @Data
    public class Info{
        private String projectName ;
        private String subProjectName ;

        public Info( String projectName , String subProjectName){
            this.projectName = projectName;
            this.subProjectName = subProjectName;
        }
    }
    private static String changePackageToReasourcePath(String packagePath){
        return basePackages.replace(".","/");
    }
}
